package com.leyou.test1.demo;

import com.leyou.test1.constants.DateTurnUtils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author : LiuDong
 * @date : Created in 2018/11/26 16:36
 */
public class DateUTCDemo {

    /**
     * 格式化日期对象
     */
    private static final DateFormat DATE_FORMAT_MIL = new SimpleDateFormat(DateTurnUtils.PATTERN_MIL);
    private static final DateFormat DATE_FORMAT_CAL = new SimpleDateFormat(DateTurnUtils.PATTERN_UTC);
    private static final DateFormat DATE_FORMAT_UTC = new SimpleDateFormat(DateTurnUtils.PATTERN_UTC);
    static {
        DATE_FORMAT_UTC.setTimeZone(TimeZone.getTimeZone("UTC"));
    }

    /**
     * 功能描述: 获取日期字符串
     * @param obj obj是Date, 或者 Long(时间戳)
     * @return dateStr, 日期字符串(yyyy-MM-dd HH:mm:ss.SSS)
     */
    public static String getDateStr(Object obj) {
        if (obj instanceof Date || obj instanceof Number)
            return DATE_FORMAT_MIL.format(obj);
        else
            throw new IllegalArgumentException("Cannot format given Object as a Date");
    }

    /**
     * 功能描述: 获取UTC格式的日期字符串
     * @param obj obj是Date, 或者 Long(时间戳)
     * @return UTCDateStr, UTC格式的日期字符串(yyyy-MM-dd'T'HH:mm:ss.SSS'Z')
     */
    public static String getUtcDateStr(Object obj) {
        if (obj instanceof Date || obj instanceof Number)
            return DATE_FORMAT_UTC.format(obj);
        else
            throw new IllegalArgumentException("Cannot format given Object as a Date");
    }
    /**
     * 功能描述: 获取UTC格式的日期字符串
     * @param calendar Calendar对象
     * @return UTCDateStr, UTC格式的日期字符串(yyyy-MM-dd'T'HH:mm:ss.SSS'Z')
     * 缺陷: 修改了Calendar对象的毫秒值
     */
    public static String getUtcDateStr(Calendar calendar) {
        int zoneOffset = calendar.get(Calendar.ZONE_OFFSET);
        int dstOffset = calendar.get(Calendar.DST_OFFSET);
        calendar.add(Calendar.MILLISECOND, - (zoneOffset + dstOffset));
        return DATE_FORMAT_CAL.format(calendar.getTimeInMillis());
    }

    /**
     * 功能描述:
     * A) 创建当前时间对象
     * B) 各自的toString()方法
     */
    public static void getCurryDateStr(){
        // 忽略对pattern的校验
        Date curDate = new Date();
        Calendar curCalendar = Calendar.getInstance();
        Timestamp curTimestamp = new Timestamp(System.currentTimeMillis());

        System.out.println("Date: " + curDate);
        System.out.println("Cale: " + curCalendar);
        System.out.println("Time: " + curTimestamp);
    }
    /**
     * 功能描述:
     * A) 创建当前时间对象
     * B) 格式化不同的对象, PS: 测试DateFormat.format()方法的用法
     * @param pattern 格式字符串
     */
    public static void getCurryDateString(String pattern){
        // 忽略对pattern的校验
        Date curDate = new Date();
        Calendar curCalendar = Calendar.getInstance();
        Timestamp curTimestamp = new Timestamp(System.currentTimeMillis());

        DateFormat dateFormat = new SimpleDateFormat(pattern);
        System.out.println("Date: " + dateFormat.format(curDate));
        System.out.println("Date: " + dateFormat.format(curDate.getTime()));
//        System.out.println("Cale: " + dateFormat.format(curCalendar));// 报错: java.lang.IllegalArgumentException: Cannot format given Object as a Date
        System.out.println("Cale: " + dateFormat.format(curCalendar.getTimeInMillis()));
        System.out.println("Time: " + dateFormat.format(curTimestamp));
        System.out.println("Time: " + dateFormat.format(curTimestamp.getTime()));
    }

}
